// pages/topay/topay.js
var app = getApp();
import {
  Http
} from '../../utils/httpClient.js';
//import{payment} from '../../utils/payment.js';

Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    token:'',
    code: '',
    shopId: '',
    storeName: '',
    price: 0,
    cart: undefined,
    delPrice: 0,
    description:undefined,
    chargeMoney: 0,
    rechargeConf:[],//充值满送数组
    idx:'',//控制选中状态
    iptvalue:'',//充值输入框的值
    /**
     * 订单号
     */
    orderNumber: 0,
    /**
     * 弹窗优惠券内容
     */
    couponData: [],
    /**
     * 优惠券数量
     */
    count: 1,
    /**
     * 订单备注信息
     */
    desc:'',

    /**
     * 支付选项卡
     * 1余额支付  2微信支付
     */
    tab:1,

    /**
     * 优惠券弹窗是否显示
     * false: 否
     */
    markCoupon: false,

    /**
     * 当前选择优惠券
     * -1 可查看是否可用优惠券 0没有可用优惠券 {}选择的优惠券内容
     */
    //curSelectCoupon: -1,

        /**
     * 输入框数据
     */
    // inputData: {
    //   input_value: "",//输入框的初始内容
    //   value_length: 0,//输入框密码位数
    //   isNext: false,//是否有下一步的按钮
    //   get_focus: true,//输入框的聚焦状态
    //   focus_class: true,//输入框聚焦样式
    //   value_num: [1, 2, 3, 4, 5, 6],//输入框格子数
    //   height: "98rpx",//输入框高度
    //   width: "604rpx",//输入框宽度
    //   see: false,//是否明文展示
    //   interval: true,//是否显示间隔格子
    // },
     /**
     * 输入密码弹窗
     */
    //maskInputPassed:false,

    address: {
      address:'xnhnjsmkscmsk',
      name:'xxxx',
      phone:'12345678999'
    }
  },

  watch: {
    address: function(newValue, data){
      console.log(newValue); // name改变时，调用该方法输出新值。
      console.log(data)	//data和newValue的值一样
    }
  },
  initData(options) {
    var _this = this;
    var httpClient = new Http();
    console.log(options);
    
    var queryBean = JSON.parse(options.cart)
    this.setData({
      shopId: options.storeId || '',
      price: options.totalMoney,
      cart: queryBean,
      storeName: options.storeName,
      delPrice: options.delPrice,
    })
    
    
    //获取充值满送配置接口，传空参
   httpClient.getChargeConf("").then((data)=>{
    this.setData({
      rechargeConf:data
    })
    console.log(this.data.rechargeConf);
   })
    
   this.getOrderInfo();
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        _this.setData({
          userName: res.userInfo.nickName,
          userAvatarUrl: res.userInfo.avatarUrl
        })
      }
    })
  },

  // 密码输入完成验证
  valueSix(e){
    var cid = Object.prototype.toString.call(this.data.curSelectCoupon).slice(8, -1) == 'object' && this.data.curSelectCoupon ? this.data.curSelectCoupon.id : '';
        var argu = {
          token: app.globalData.token,
          orderNumber: this.data.orderNumber,
          desc:this.data.desc
        }
        httpClient.postAmountOrder(argu)
        .then((res)=>{
          console.log('支付成功');
          wx.showToast({
            title: '支付成功'
          });
          this.closeMarkPassword();
          wx.navigateTo({
            url: "../pay_success/pay_success?price="+_this.data.chargeMoney
          })
        })
        .catch((e)=>{
          console.log('支付失败');
          var str = e.msg?e.msg:'支付失败';
          wx.showToast({
            title: str
          });
          //重新开启弹窗
          this.closeMarkPassword();
          this.openMarkPassword();
        })
  },
  onLoad: function (options) {
    //var price = options.totalMoney || 0;
    
    this.initData(options);

    this.setData({
      price:price1+delPrice
    })
    getApp().setWatcher(this.data, this.watch); // 设置监听器
      this.setData({
        address:''
    })
  },
  /**
     * 获取订单信息
     */
  getOrderInfo(){
    var httpClient=new Http();
    var _this=this;
    var argu = {
      orderNumber: this.data.orderNumber,
      token: app.globalData.token
    };
    httpClient.getOrederSelect(argu).then((data) => {
      console.log("调取信息",data);
      _this.setData({
        title: data.title,
        price: (data.price / 100).toFixed(2),
        coupon_price: data.coupon_price
      })
    })
  },
 
  getValue(e) {
    this.setData({
      price: 43
    })
  },
  
  descChange: function(e){
    this.setData({
      description: e.detail.value
    })
 },
 tabChange(e){
  var tab = e.currentTarget.dataset.tab;
  this.setData({
    tab:parseInt(tab)
  })
},

   
  /**
   * 点击支付
   */
  pay(){
    switch (this.data.tab) {
      case 1:
        this.paysuccess();
        break;
      case 2:
        this.paysuccess();
        break;
      default:
        break;
    }
  },

  /**
   * 微信支付调用(需要判断是否有优惠券)
   */
  paysuccess:function(){
    var that = this;
    // var houseId = that.data.houseId;
    // var cart = that.data.cart;
    // var total = that.data.totalprice;
    // var integral = that.data.jifen;
    //var url = 'http://localhost:8080/community/pay/payok1';
    console.log(that.data.cart)
    var token = app.getToken()
    // wx.request({
    //     url: 'http://localhost:8080/community/ec/pay',
    //     method:'POST',
    //     data: {
    //         cart : this.data.cart,
    //         storeId : this.data.shopId,
    //         price : this.data.price,
    //         delPrice: this.data.delPrice,
    //         payment:this.data.tab,
    //         address: this.data.address.address+this.data.address.name+this.data.address.phone,
    //         description:this.data.description,
            
    //       },
    //     header:{
    //       "Content-Type": "application/json",
    //       'Authorization':  token
    //     },
    //     success:function(res){
    //       // console.log("userInfo" + JSON.stringify(res))
    //       wx.navigateBack({
    //         delta: 1 , //小程序关闭当前页面返回上一页面
    //         success (res) {
    //           let page = getCurrentPages().pop()
    //           if (page == undefined || page == null) {
    //             return
    //           }
    //           page.onLoad()
    //         }
    //       })
    //       console.log(res.data);
    //       wx.showToast({
    //         title: '支付成功',
    //         icon: 'success',
    //         duration: 2000//持续的时间
    //       })
    //     },
    //   }) 
    app.sendPostRequest("/community/ec/pay", {
    cart : this.data.cart,
    storeId : this.data.shopId,
    price : this.data.price,
    delPrice: this.data.delPrice,
    payment:this.data.tab,
    address: this.data.address.address+this.data.address.name+this.data.address.phone,
    description:this.data.description,
    
  },(res) => {
    wx.navigateBack({
      delta: 1 , //小程序关闭当前页面返回上一页面
      success (res) {
        let page = getCurrentPages().pop()
        if (page == undefined || page == null) {
          return
        }
        page.onLoad()
      }
    })
    console.log(res);
    wx.showToast({
      title: '支付成功',
      icon: 'success',
      duration: 2000//持续的时间
    })
  })
},

  
  
  

  
})